package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppDynamic;
import com.vf.admin.miaodong.entity.AppInvitationRecord;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterInvitationRecordService;
import com.vf.admin.miaodong.service.AfterSetupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 邀请管理
 *
 * @author anxiang
 */
@CrossOrigin
@Controller
@RequestMapping("/after/invitation")
public class AfterInvitationAdminController extends BaseController {

    @Autowired
    AfterInvitationRecordService afterInvitationRecordService;

    @Autowired
    AfterSetupService afterSetupService;
    @Autowired
    AppUserService appUserService;


    /**
     * 获取所有用户列表信息
     *
     * @param pageBean 分页对象
     * @param pnick     用户昵称
     * @param pphone    用户手机号
     * @param start    时间范围-前
     * @param end      时间范围-后
     * @return
     */
    @RequestMapping("/invitationList")
    public String list(SplitPageBean pageBean, String pnick, String pphone, String start, String end) {
        try {
            //检索条件
            Map<String, Object> parMap = new HashMap<>();
            //用户昵称
            parMap.put("pnick", pnick);
            //用户手机号
            parMap.put("pphone", pphone);
            //时间范围-前
            parMap.put("startDate", start);
            //时间范围-后
            parMap.put("endDate", end);
            //根据检索条件查询数据
            List<AppInvitationRecord> list = afterInvitationRecordService.findList(parMap, pageBean);
            String userArr = "";
            for (AppInvitationRecord record : list) {
                userArr += "," + record.getUserid();
            }
            if(!isNull(userArr)){
                userArr = userArr.substring(1);
            }
            //批量查询发布人信息
            Map<Integer,AppUser> userMap=new HashMap<>(16);
            if(!isNull(userArr)){
                List<AppUser> userList = appUserService.getPiliang(userArr);
                for (AppUser user1 : userList) {
                    userMap.put(user1.getId(),user1);
                }
            }
            for (AppInvitationRecord invitationRecord : list) {
                AppUser user2=userMap.get(invitationRecord.getUserid());
                invitationRecord.setLeijichongzhi(user2.getChongzhimoney());
                invitationRecord.setShimingstate(user2.getStatus());
            }
            Map<Integer, String> userStatusMap = Constants.getUserStatusMap();
            request.setAttribute("userStatusMap", userStatusMap);
            request.setAttribute("list", list);
            request.setAttribute("inv", parMap);
            request.setAttribute("splitPage", pageBean);
            request.setAttribute("pnick", pnick);
            request.setAttribute("pphone", pphone);
            request.setAttribute("start", start);
            request.setAttribute("end", end);
            return this.sucess("/web/after/invitation/invitationList");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }


}
